﻿using Axis.BusinessObject.Research;
using Axis.Dao.Research;
using Axis.Web.Administrator.Global;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Axis.Web.Views.Home
{
    public partial class ucSliderMini : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<CGift> list = GetGift();
                ltrGift.Text = GetGiftHtmls(list);
            }
        }
        protected string GetGiftHtmls(IList<CGift> pGifts)
        {
            StringBuilder ret = new StringBuilder();
            if (pGifts != null && pGifts.Count > 0)
            {
                //<div class="jshowoff-item"></div>
                string temp = string.Empty;
                int count = 0;
                foreach (CGift gift in pGifts)
                {
                    count++;
                    int index = pGifts.IndexOf(gift);
                    temp += GetGiftHtml(gift);
                    if (count == 1 || (index == pGifts.Count - 1))
                    {
                        ret.AppendFormat("<div class='jshowoff-item'>{0}</div>", temp);
                        count = 0;
                        temp = "";
                    }

                }
            }
            return ret.ToString();
        }
        protected string GetGiftHtml(CGift pGift)
        {
            StringBuilder ret = new StringBuilder();
            if (pGift != null)
            {
                int id = pGift.ID;
                string name = pGift.Name;
                string desc = pGift.Description;
                string image1 = pGift.Image;
                DateTime endDate = pGift.CreatedDateTime;
                string ProjectID = pGift.ProjectID.ToString();
                string html = GetGiftTemplate();
                html = html.Replace("[ID]", id.ToString());
                html = html.Replace("[Image]", image1);
                html = html.Replace("[Name]", name);
                //html = html.Replace("[Date]", endDate.ToString("dd/MM/yyyy"));
                html = html.Replace("[Date]","");
                html = html.Replace("[ProjectID]", ProjectID);
                ret.Append(html);
            }
            return ret.ToString();
        }
        protected IList<CGift> GetGift()
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            root.SetAttribute("Context", "FrontEnd");
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("RowsPerPage", "20");
            string inputValue = doc.OuterXml;
            CResearchDao cResearchDao = new CResearchDao();
            IList<CGift> list = cResearchDao.GetGift(inputValue);
            return list;

        }
        protected string GetGiftTemplate()
        {
            string ret = string.Empty;
            StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath("~/Views/template/gift_slide_mini.txt"));
            ret = streamReader.ReadLine();
            while (streamReader.ReadLine() != null)
            {
                ret += streamReader.ReadLine();
            }
            streamReader.Close();
            return ret;
        }
    }
}